package com.cfw.queue;

import java.util.Scanner;


public class ArrayQueueDemo {

	public static void main(String [] agrs) {
		
		Scanner scanerScanner = new Scanner(System.in);
		
		boolean loop = true;
		
		ArrayQueue queue = new ArrayQueue(3);
		
		while (loop) {
			char key = scanerScanner.next().charAt(0);
			
			switch (key) {
				case 'a':
					System.out.println("请输入一个数");
					int num = scanerScanner.nextInt();
					queue.addQueue(num);
					break;
				case 'e':
					loop = false;
					break;
				case 's':
					queue.showQueue();
					break;
				case 'g':
					System.out.println(queue.getItem());
					break;
				case 'h':
					System.out.println(queue.getHead());
					break;
				default:
					break;
				}
			
		}
		
	}
	
	
	
	
}
/**
 * 通过数组模拟 队列
 * 
 * maxSize 队列最大长度
 * front 队列前面元素的前一位
 * rear 队尾 
 * 
 * 这种方法模拟队列，去除元素后，元素没有删除，或导致无法添加元素
 * 可以采取环形数组，修改 front 和 rear 和 取模改进
  */

class ArrayQueue {
	private int maxSize;
	private int front;
	private int rear;
	private int array[];
	
	public ArrayQueue(int maxSize) {
		this.maxSize = maxSize;
		array = new int[this.maxSize];
		this.front =-1;
		this.rear = -1;
	}
	
	//判断队列是否为空
	public boolean isEmpty() {
		
		return this.rear == this.front;
		
	}
	
	//判断队列是否满了
	public boolean isFull() {
		return this.rear == this.maxSize-1;
	}
	
	//加元素
	
	public void addQueue(int item) {
		if (this.isFull()) {
			System.out.println("队列满了");
			return;
		}
		
		this.rear++;
		this.array[this.rear] = item;
	}
	
	
	//出队列，移除队列
	public int getItem(){
		if(this.isEmpty()) {
			System.out.println("队列为空");
			return -1;
		}
		this.front++;
		return this.array[this.front];
	}
	
	//查看队头，不删除
	public int getHead() {
		if(this.isEmpty()) {
			System.out.println("队列为空");
			return -1;
		}
		return this.array[this.front+1];
	}
	
	//显示队列
	public void showQueue() {
		if(this.isEmpty()) {
			System.out.println("队列为空");
			return;
		}
		
		for (int i = 0; i < this.array.length; i++) {
			System.out.printf("arr[%d]=%d\t", i,this.array[i]);
		}
	}
}